package com.cx.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.entity.Article;
import com.cx.mapper.ArticleMapper;
import com.cx.service.IArticleService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@com.alibaba.dubbo.config.annotation.Service //：发布接口服务
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Override
    public IPage<Article> getArticleListPageConditions(Map<String, Object> params) {
        IPage<Article> page = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Map<String, String> paramsMap = null;
        if (params.get("searchParams") != null) {
            String startDate = null,
                    endDate = null;
//          将获取的json格式的字符串转为Java中的对象
            paramsMap = (Map) (JSON.parse((String) params.get("searchParams")));
//          切割日期字符串
            if (!"".equals(paramsMap.get("created"))) {
                startDate = (paramsMap.get("created").split("~"))[0];
                endDate = (paramsMap.get("created").split("~"))[1];
            }

            articleLambdaQueryWrapper.eq(!"".equals(paramsMap.get("id")), Article::getId, paramsMap.get("id"))
                    .eq(!"".equals(paramsMap.get("articleUsernameId")), Article::getArticleUsernameId, paramsMap.get("articleUsernameId"))
                    .like(!"".equals(paramsMap.get("title")), Article::getTitle, paramsMap.get("title"))
                    .like(!"".equals(paramsMap.get("categories")), Article::getCategories, paramsMap.get("categories"))
                    .between(!"".equals(paramsMap.get("created")), Article::getCreated, startDate, endDate)
            ;
        }
        return this.baseMapper.getArticleListPageConditions(page, articleLambdaQueryWrapper);

    }
}
